package me.dr0n3.skyTravel;

import java.util.LinkedList;
import org.bukkit.ChatColor;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.block.Action;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.event.player.PlayerQuitEvent;

/**
 * @author dr0n3
 * @date 21.03.2012
 */
public class SkyListener implements Listener
{
    SkyTravel plugin;
    
    public SkyListener(SkyTravel instance)
    {
        plugin = instance;
    }
    
    public void updatePlayerLocation()
    {
        plugin.lPlayersInPorts = getPlayersInPorts();
        
        for(Player p : plugin.getServer().getOnlinePlayers())
        {
            for(Port po : plugin.lPorts)
            {
                if(po.isInPort(p))
                {
                    if(!plugin.lLastPlayersInPorts.contains(p))
                    {
                        p.sendMessage(ChatColor.LIGHT_PURPLE + "Willkommen an Bord.");
                        p.sendMessage(ChatColor.LIGHT_PURPLE + "Reise mit /depart ab.");
                    }
                }
            }
        }
        
        plugin.lLastPlayersInPorts = convertPlayersInPortsToLast();     
    }
    
    public LinkedList<Player> convertPlayersInPortsToLast()
    {
        LinkedList<Player> lp = new LinkedList<>();
        
        for(Player p : plugin.lPlayersInPorts)
        {
            lp.add(p);
        }
        
        return lp;
    }
    
    public LinkedList<Player> getPlayersInPorts()
    {
       LinkedList<Player> lp = new LinkedList<>();
       
       for(Player p : plugin.getServer().getOnlinePlayers())
       {
           for(Port po : plugin.lPorts)
           {
               if(po.isInPort(p))
               {
                    lp.add(p);
               }
           }
       }
       
       return lp;
    }
    
    @EventHandler
    public void onPlayerQuit(PlayerQuitEvent e)
    {
        if(plugin.lToggles.contains(e.getPlayer()))
            plugin.lToggles.remove(e.getPlayer());
    }
    
    @EventHandler
    public void onPlayerInteract(PlayerInteractEvent e)
    {
        Player player = e.getPlayer();
        
        if(plugin.lToggles.contains(player))
        {
            if(e.getAction() == Action.LEFT_CLICK_BLOCK)
            {
                if(e.getMaterial().getId() == plugin.toolID)
                {
                    plugin.sendBackendMessage(player, false, "Erste Ecke gesetzt.");
                    
                    plugin.hmLeftClick.put(player, player.getLocation());
                }
            }
            else if(e.getAction() == Action.RIGHT_CLICK_BLOCK)
            {
                if(e.getMaterial().getId() == plugin.toolID)
                {
                    plugin.sendBackendMessage(player, false, "Zweite Ecke gesetzt.");
                    
                    plugin.hmRightClick.put(player, player.getLocation());
                }
            }
        }
    }
}
